﻿   
    var currentProcessStr="";   //进度
    var dataSourceList="";      //数据源列表
    var currentTableName="";    //当前表
    var isEnd=false;            //结束标志
    var flag;                   //标志
    var ztTableName="";         //专题表名
    var queryValue="";          //表达式
    
    function isLastDataSource() //是否是最后一个数据表
    {
        var list=dataSourceList.split(',');
        if(list[list.length-1]==currentTableName)
        {
            return true;
        }
        return false;
    }
    
    function getProcessStr()    //获取进度条参数
    {
        Patent.Topic.GetProcessStr(ztTableName,getProcessStr_callback);
    }
    
    function getProcessStr_callback(response)    //获取进度条参数回调函数
    {
        currentProcessStr=response.value;
        bindProcessStr();
    }
    
    function bindProcessStr()   //绑定进度条
    {
        // alert(currentProcessStr);
        if(isBlank(currentProcessStr)&&isLastDataSource())
        //if(isBlank(currentProcessStr))
        {
            isEnd=true;
            processBar.updateProgress(1,'完毕!');
            parseEnd();
            return ;
        }
        else
        {
            isEnd=false;
            var currentIndex=parseFloat(currentProcessStr.split(',')[0]);
            var sum=parseFloat(currentProcessStr.split(',')[1]);
            currentTableName=currentProcessStr.split(',')[2];
            p=currentIndex/sum;
            var num= parseInt(p*100)+'%';
            var str= num+' ('+currentIndex+'/'+sum+') '+parseTableName(currentTableName);
            processBar.updateProgress(p,str);
            parseEnd();
            return;
        }
    }
    
    function parseTableName(str)    //表名称转换
    {
        var returnName='';
        switch(str)
        {
            case 'uspatent' : 
            returnName='美国'; break;
            case 'jppatent' : 
            returnName='日本'; break;
            case 'gbpatent' : 
            returnName='英国'; break;
            case 'depatent' : 
            returnName='德国'; break;                
            case 'frpatent' : 
            returnName='法国'; break;
            case 'chpatent' : 
            returnName='瑞士'; break;
            case 'wopatent' : 
            returnName='世界'; break;  
            case 'eppatent' : 
            returnName='欧洲'; break;
            case 'fmzl' : 
            returnName='发明专利'; break;
            case 'wgzl' : 
            returnName='外观设计'; break;
            case 'syxx' : 
            returnName='实用新型'; break;  
            default:
            returnName='';
            break;                                                                               
                                       
        }
        return returnName;
    }
    
    function parseEnd()    //中断进度
    { 
        //alert(isEnd);
        if(isEnd)
        {
             window.clearInterval(flag);
             document.getElementById('Button1').disabled=false;
        }
    }
    
    function getQueryString()    //获取查询表达式
    {
        var queryString="";
        queryString=getQueryValue()+"_"+getQueryDataSource();
        return queryString;  
    }
    
    function getElementValue(id)    //获取HTML标签的值
    {
        var value="";
        var element=document.getElementById(id);
        if(element)
        {
             value=element.value;
        }
        return value;
    }
    
    function getCheckBoxValue(id)    //获取CheckBox的值
    {
        var value=false;
        var element=document.getElementById(id);
        if(element)
        {
             value=element.checked;
        }
        return value;
    }
    
    function isBlank(value)         //判断值是否为空
    {
        if(value==null||value=="") return true;
        return false;
    }
    
    function getQueryValue()        //获取查询条件连接表达式
    {
         var sqh=getElementValue("tb_sqh");
         var sqrq_lower_1=getElementValue("tb_sqrq_lower_1");
         var sqrq_upper_1=getElementValue("tb_sqrq_upper_1");
         var ggh=getElementValue("tb_ggh");
         var ggrq_lower=getElementValue("tb_ggrq_lower");
         var ggrq_upper=getElementValue("tb_ggrq_upper");
         var zlh=getElementValue("tb_zlh");
         var sqrq_lower=getElementValue("tb_sqrq_lower");
         var sqrq_upper=getElementValue("tb_sqrq_upper");
         var mc=getElementValue("tb_mc");
         var fmr=getElementValue("tb_fmr");
         var sqr=getElementValue("tb_sqr");
         var dz=getElementValue("tb_dz");
         var zfl=getElementValue("tb_zfl");
         var fl=getElementValue("tb_fl");
         var qlyq=getElementValue("tb_qlyq");
         var zy=getElementValue("tb_zy");
         var yxq=getElementValue("tb_yxq");
         
         var returnValue="";
//         if(isBlank(sqh)==false)
//         {
//              if(isBlank(returnValue))returnValue=returnValue+"#A="+sqh;
//              else returnValue=returnValue+" and #A="+sqh;
//         }
//         
         if(isBlank(sqrq_lower_1)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#A>="+sqrq_lower_1;
              else returnValue=returnValue+" and #A>="+sqrq_lower_1;
         }
         if(isBlank(sqrq_upper_1)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#A<="+sqrq_upper_1;
              else returnValue=returnValue+" and #A<="+sqrq_upper_1;
         }
         
//         if(isBlank(ggh)==false)
//         {
//              if(isBlank(returnValue))returnValue=returnValue+"#C="+ggh;
//              else returnValue=returnValue+" and #C="+ggh;
//         }
         
         if(isBlank(ggrq_lower)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#B>="+ggrq_lower;
              else returnValue=returnValue+" and #B>="+ggrq_lower;
         }
         if(isBlank(ggrq_upper)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#B<="+ggrq_upper;
              else returnValue=returnValue+" and #B<="+ggrq_upper;
         }
         
//         if(isBlank(zlh)==false)
//         {
//              if(isBlank(returnValue))returnValue=returnValue+"#E="+zlh;
//              else returnValue=returnValue+" and #E="+zlh;
//         }
         
         if(isBlank(sqrq_lower)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#C>="+sqrq_lower;
              else returnValue=returnValue+" and #C>="+sqrq_lower;
         }
         if(isBlank(sqrq_upper)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#C<="+sqrq_upper;
              else returnValue=returnValue+" and #C<="+sqrq_upper;
         }
         
//         if(isBlank(mc)==false)
//         {
//              if(isBlank(returnValue))returnValue=returnValue+"#G=%"+mc+"%";
//              else returnValue=returnValue+" and #G=%"+mc+"%";
//         }
         
         if(isBlank(fmr)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#D=%"+fmr+"%";
              else returnValue=returnValue+" and #D=%"+fmr+"%";
         }
         
          
         if(isBlank(sqr)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#E=%"+sqr+"%";
              else returnValue=returnValue+" and #E=%"+sqr+"%";
         }
         
          
         if(isBlank(dz)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#F=%"+dz+"%";
              else returnValue=returnValue+" and #F=%"+dz+"%";
         }
         
         if(isBlank(zfl)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#G="+zfl+"%";
              else returnValue=returnValue+" and #G="+zfl+"%";
         }
         
         if(isBlank(fl)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#H="+fl+"%";
              else returnValue=returnValue+" and #H="+fl+"%";
         }
         
                
         if(isBlank(qlyq)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#I=%"+qlyq+"%";
              else returnValue=returnValue+" and #I=%"+qlyq+"%";
         }
         
         if(isBlank(zy)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#J=%"+zy+"%";
              else returnValue=returnValue+" and #J=%"+zy+"%";
         }
         
         if(isBlank(yxq)==false)
         {
              if(isBlank(returnValue))returnValue=returnValue+"#K=%"+yxq+"%";
              else returnValue=returnValue+" and #K=%"+yxq+"%";
         }
         
         return returnValue;
    }
    
    function getQueryDataSource()     //获取数据查询范围
    {
        var table = "";
        if(false)
        {
             table="chpatent,uspatent,jppatent,gbpatent,depatent,frpatent,wopatent,eppatent";
        }
        else
        {
             if(document.getElementById("cb_us").checked)
             {
                  if(table!="")table+=","; 
                  table+="uspatent";
             }
             if(document.getElementById("cb_jp").checked)
             {
                  if(table!="")table+=",";
                  table+="jppatent";
             }
             if(document.getElementById("cb_gb").checked)
             {
                  if(table!="")table+=",";
                  table+="gbpatent";
             }
             if(document.getElementById("cb_de").checked)
             {
                  if(table!="")table+=",";
                  table+="depatent";
             }
             if(document.getElementById("cb_fr").checked)
             {
                  if(table!="")table+=",";
                  table+="frpatent";
             }
             if(document.getElementById("cb_cn").checked)
             {
                  if(table!="")table+=",";
                  table+="chpatent";
             }
             if(document.getElementById("cb_wipo").checked)
             {
                  if(table!="")table+=",";
                  table+="wopatent";
             }
             if(document.getElementById("cb_ep").checked)
             {
                  if(table!="")table+=",";
                  table+="eppatent";
             }
              
        }
        
        if(getCheckBoxValue("cb_wgsj"))    //中国专项
        {
             if(table!="")table+=",";
             table+="wgzl";
        }
        if(getCheckBoxValue("cb_fmzl"))
        {
             if(table!="")table+=",";
             table+="fmzl";
        }
        if(getCheckBoxValue("cb_syxx"))
        {
             if(table!="")table+=",";
             table+="syxx";
        }
        
        if(table!="")currentTableName=table.split(",")[0];
        return table;
    }
    
    function submitTopic()
    {

        dataSourceList=getQueryDataSource();
        queryValue=getQueryValue();
        
        var ljysValue=getElementValue("tb_ljys");
        if(isBlank(ljysValue)==false)
        {
           queryValue=ljysValue;
        }
        if(dataSourceList=="")
        {
            error("必须选择查询范围!");
            return false;
        }
        document.getElementById('Button1').disabled=true;
       // alert(returnValue);
        var ztmc=getElementValue("tb_ztmc");
        Patent.Topic.CreateTopic(ztmc,queryValue+"_"+dataSourceList,submitTopic_callback);
        return false;
    }
    
    function submitTopic_callback(response)
    {
        var value=response.value;
       // alert(value);
        if(isBlank(value))
        {
            error("发生错误!");
            return ;
        }
        ztTableName=value.split(',')[1];
        Patent.Topic.ProcessQuery(value,queryValue+"_"+dataSourceList,ProcessQuery_callback);
        
        processBar.updateProgress(0,'等待........');
        window.setTimeout(startProcessing,5000);
    }
    
    function startProcessing()
    {
        flag=window.setInterval(getProcessStr,500);
    }
    function ProcessQuery_callback(response)
    {
        isEnd=true;
        parseEnd();
    }
    function error(type)
    {
        //alert(type);
           Ext.MessageBox.show({
           title: '错误',
           msg: type,
           width:250,
           buttons: Ext.MessageBox.OK,
           icon: Ext.MessageBox.ERROR
       });
    }
    
    function addLogicSelected()
    {
        var logicElement=document.getElementById('logic_select');
        var logicValue=logicElement.options[logicElement.selectedIndex].innerHTML;
        var logicTextElement=document.getElementById('tb_ljys');
        logicTextElement.value+=' '+logicValue+ ' ';
    }